﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XML
{
    public class XML
    {
        XmlDocument xmlDocument = new XmlDocument();
        string documentPath;
        string headerNode;

        public void Load_XML(string FileName, string HeaderNode)
        {
            string[] filepath = FileName.Split('\\');

            if ((FileName == null) || (FileName == ""))
                documentPath = "C:\\xml.dll.log";
            else
                documentPath = FileName;
            if ((HeaderNode == null) || (HeaderNode == ""))
                headerNode = filepath[filepath.Length - 1];
            else
                headerNode = HeaderNode;

            try { xmlDocument.Load(documentPath); }
            catch { xmlDocument.LoadXml("<" + headerNode.Substring(0, headerNode.Length - 1) + "></" + headerNode.Substring(0, headerNode.Length - 1) + ">"); }
        }


        public string GetSetting(string xPath, string defaultValue)
        {
            XmlNode xmlNode = xmlDocument.SelectSingleNode(headerNode + xPath);
            if (xmlNode != null)
                return xmlNode.InnerText;
            else
                return defaultValue;
        }
        public int GetSetting(string xPath, int defaultValue)
        {
            return Convert.ToInt32(GetSetting(xPath, Convert.ToString(defaultValue)));
        }
        public long GetSetting(string xPath, long defaultValue)
        {
            return Convert.ToInt64(GetSetting(xPath, Convert.ToString(defaultValue)));
        }
        public double GetSetting(string xPath, double defaultValue)
        {
            return Convert.ToDouble(GetSetting(xPath, Convert.ToString(defaultValue)));
        }
        public bool GetSetting(string xPath, bool defaultValue)
        {
            return Convert.ToBoolean(GetSetting(xPath, Convert.ToString(defaultValue)));
        }

        public void SetSetting(string xPath, string value)
        {
            XmlNode xmlNode = xmlDocument.SelectSingleNode(headerNode + xPath);
            if (xmlNode == null)  
                xmlNode = CreateMissingNode(headerNode + xPath);
            xmlNode.InnerText = value;
            xmlDocument.Save(documentPath);
        }
        public void SetSetting(string xPath, short value)
        {
            SetSetting(xPath, Convert.ToString(value));
        }
        public void SetSetting(string xPath, int value)
        {
            SetSetting(xPath, Convert.ToString(value));
        }
        public void SetSetting(string xPath, long value)
        {
            SetSetting(xPath, Convert.ToString(value));
        }
        public void SetSetting(string xPath, double value)
        {
            SetSetting(xPath, Convert.ToString(value));
        }
        public void SetSetting(string xPath, bool value)
        {
            SetSetting(xPath, Convert.ToString(value));

        }

        private XmlNode CreateMissingNode(string xPath)
        {
            string[] xPathSections = xPath.Split('/');
            string currentXPath = "";
            XmlNode testNode = null;
            XmlNode currentNode = xmlDocument.SelectSingleNode(headerNode.Substring(0,headerNode.Length - 1));

            foreach (string xPathSection in xPathSections)
            {
                currentXPath += xPathSection;
                testNode = xmlDocument.SelectSingleNode(currentXPath);
                if (testNode == null) 
                    currentNode.InnerXml += "<" + xPathSection + "></" + xPathSection + ">";
                currentNode = xmlDocument.SelectSingleNode(currentXPath);
                currentXPath += "/";
            }
            return currentNode;
        }
    }
}
